package wxapp

import (
    "fmt"
	"errors"
	"wxsdk/miniapp/core"
	"github.com/astaxie/beego"
)

// 绑定微信用户为小程序体验者
type BindTesterRequest struct {
	WechatId    string `json:"wechatid"`
}

func (r *BindTesterRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/bind_tester?access_token=%s",token)
}

type BindTesterResponseResult struct {
	*core.ErrorResponse
	*BindTesterResponse
}

type BindTesterResponse struct {
	
}

// 解除绑定小程序的体验者
type UnBindTesterRequest struct {
	WechatId    string `json:"wechatid"`
}

func (r *UnBindTesterRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/unbind_tester?access_token=%s",token)
}


type UnbindTesterResponseResult struct {
	*core.ErrorResponse
	*UnbindTesterResponse
}

type UnbindTesterResponse struct {
	
}


// 绑定微信用户为小程序体验者
func  BindTester(authorizerAccessToken, wechatId string) (int, error){
	client := core.GetPostJSONClient()
	var rsp BindTesterResponseResult
	var req = new(BindTesterRequest)
	req.WechatId = wechatId
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("POST", serverUrl, req, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return -5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return 0, nil
}

// 解除绑定小程序的体验者
func  UnbindTester(authorizerAccessToken, wechatId string) (int, error){
	client := core.GetPostJSONClient()
	var rsp UnbindTesterResponseResult
	var req = new(UnBindTesterRequest)
	req.WechatId = wechatId
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("POST", serverUrl, req, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return -5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return 0, nil
}
